#include <stdio.h>
#include <string.h>
typedef struct item_t {
	char *key;
	char *value;
} ITEM;

/* *clear all String space */

char *strClearAllSpace(char *pstr) {

	int i = 0;
	i = strlen(pstr) - 1;
	char *tmpstr;
	tmpstr = (char *) malloc(strlen(pstr)*sizeof(char));
	memset(tmpstr, 0, sizeof(tmpstr));

	char *p = tmpstr;
	while (1) {

		if(!pstr[i]) break;

		if(isspace(pstr[i])) {
			*p = pstr[i];
			p++;
		}
	}
	return tmpstr;
}
//check String is OK not head is * or # and not match a=b format
int checkValIsOK(char * valstr) {

	char *p = strClearAllSpace(valstr);
	if(*p == '*' ||*p == '#') return 0;

	char *ptr = strchr(valstr, '=');
	if(ptr) return 1;
	return 0;
}

/* *从配置文件的一行读出key或value,返回item指针 *line--从配置文件读出的一行 */
int get_item_from_line(char *line, ITEM *item)
{
	char *p = strtrim(line);
	int len = strlen(p);
	if(len <= 0) {
		return 1;//空行
	}
	else if(p[0]=='#') {return 2;}
	else {char *p2 = strchr(p, '=');
		*p2++ = '\0';
		item->key = (char *)malloc(strlen(p) + 1);
		item->value = (char *)malloc(strlen(p2) + 1);
		strcpy(item->key,p);
		strcpy(item->value,p2);
	}
	return 0;
	//查询成功
}

int file_to_items(const char *file, ITEM *items, int *num) {
	char line[1024];
	FILE *fp;
	fp = fopen(file,"r");
	if(fp == NULL)
	return 1;
	int i = 0;
	while(fgets(line, 1023, fp)) {
		char *p = strClearAllSpace(line);
		if(!checkValIsOK(p)) continue;

		char *p2 = strchr(p, '=');
		/*这里认为只有key没什么意义*/
		if(p2 == NULL)
			continue;
		*p2++ = '\0';
		items[i].key = (char *)malloc(strlen(p) + 1);
		items[i].value = (char *)malloc(strlen(p2) + 1);
		strcpy(items[i].key,p);
		strcpy(items[i].value,p2);
		i++;
	}
	(*num) = i;
	fclose(fp);
	return 0;
}

int main(int argc, char *argv[]) {

	printf("begin work!");
	printf("end work!");
}
